export default function () {
  // 渐入
  function fadeIn(element: HTMLElement, duration = 200) {
    let start: null | number = null

    const animate = (timestamp: number) => {
      if (!start) start = timestamp
      const progress = timestamp - start
      const opacity = Math.min(progress / duration, 1)
      element.style.opacity = opacity.toString()
      if (opacity < 1) {
        requestAnimationFrame(animate)
      }
    }

    requestAnimationFrame(animate)
  }
  //   渐隐
  function fadeOut(element: HTMLElement, duration = 200) {
    let start: null | number = null
    const animate = (timestamp: number) => {
      if (!start) start = timestamp
      const progress = timestamp - start
      const opacity = Math.max(1 - progress / duration, 0)
      element.style.opacity = opacity.toString()
      if (opacity > 0) {
        requestAnimationFrame(animate)
      }
    }
    requestAnimationFrame(animate)
  }
  return {
    fadeIn,
    fadeOut
  }
}
